#coding=utf-8
#import libs 
import sys
import os
from   os.path import abspath, dirname
sys.path.insert(0,abspath(dirname(__file__)))
import ReplaceProjectStr_cmd
import ReplaceProjectStr_sty
import Fun
import EXUIControl
EXUIControl.FunLib = Fun
EXUIControl.G_ExeDir = Fun.G_ExeDir
EXUIControl.G_ResDir = Fun.G_ResDir
import tkinter
from   tkinter import *
import tkinter.ttk
import tkinter.font
from   PIL import Image,ImageTk

#Add your Varial Here: (Keep This Line of comments)
#Define UI Class
class  ReplaceProjectStr:
    def __init__(self,root,isTKroot = True,params=None):
        uiName = Fun.GetUIName(root,self.__class__.__name__)
        self.uiName = uiName
        Fun.Register(uiName,'UIClass',self)
        self.root = root
        self.configure_event = None
        self.isTKroot = isTKroot
        self.firstRun = True
        Fun.G_UIParamsDictionary[uiName]=params
        Fun.G_UICommandDictionary[uiName]=ReplaceProjectStr_cmd
        Fun.Register(uiName,'root',root)
        style = ReplaceProjectStr_sty.SetupStyle(isTKroot)
        self.UIJsonString = '{"Version": "1.0.0", "UIName": "ReplaceProjectStr", "Description": "", "WindowSize": [812, 1052], "WindowPosition": "Center", "WindowHide": false, "WindowResizable": false, "WindowTitle": "替换项目中py文件文本", "DarkMode": false, "BorderWidth": 0, "BorderColor": "#ffffff", "DropTitle": false, "DragWindow": false, "MinSize": [0, 0], "ResolutionScaling": true, "PopupDebugDialog": false, "TransparentColor": null, "RootTransparency": 255, "ICOFile": "D:/PyMe1.4.8.5/ReplaceProjectStr/Resources/Tool.ico", "ICOMode": "File", "WinState": 1, "WinTopMost": false, "BGColor": "#EFEFEF", "GroupList": {}, "WidgetList": [{"Type": "Form", "Index": 1, "AliasName": "Form_1", "BGColor": "#EFEFEF", "Size": [812, 1052], "EventList": {"Load": "Form_1_onLoad"}}, {"Type": "Entry", "Index": 2, "AliasName": "project_dir", "ParentName": "Form_1", "PlaceInfo": [258, 34, 320, 48, "nw", true, false], "Visible": true, "Size": [320, 48], "BGColor": "#FFFFFF", "BGColor_ReadOnly": "#EFEFEF", "FGColor": "#000000", "InnerBorderColor": "#000000", "TipFGColor": "#888888", "Relief": "sunken"}, {"Type": "Entry", "Index": 6, "AliasName": "old_str", "ParentName": "Form_1", "PlaceInfo": [258, 123, 320, 48, "nw", true, true], "Visible": true, "Size": [320, 48], "BGColor": "#FFFFFF", "BGColor_ReadOnly": "#EFEFEF", "FGColor": "#000000", "InnerBorderColor": "#000000", "TipFGColor": "#888888", "Relief": "sunken"}, {"Type": "Entry", "Index": 7, "AliasName": "new_str", "ParentName": "Form_1", "PlaceInfo": [257, 208, 321, 48, "nw", true, true], "Visible": true, "Size": [321, 48], "BGColor": "#FFFFFF", "BGColor_ReadOnly": "#EFEFEF", "FGColor": "#000000", "InnerBorderColor": "#000000", "TipFGColor": "#888888", "Relief": "sunken"}, {"Type": "Label", "Index": 3, "AliasName": "Label_1", "ParentName": "Form_1", "PlaceInfo": [71, 34, 142, 48, "nw", true, true], "Visible": true, "Size": [142, 48], "BGColor": "#EFEFEF", "Text": "项目目录", "FGColor": "#000000"}, {"Type": "Label", "Index": 4, "AliasName": "Label_2", "ParentName": "Form_1", "PlaceInfo": [69, 123, 146, 48, "nw", true, true], "Visible": true, "Size": [146, 48], "BGColor": "#EFEFEF", "Text": "旧字符", "FGColor": "#000000"}, {"Type": "Label", "Index": 5, "AliasName": "Label_3", "ParentName": "Form_1", "PlaceInfo": [70, 208, 145, 48, "nw", true, true], "Visible": true, "Size": [145, 48], "BGColor": "#EFEFEF", "Text": "新字符", "FGColor": "#000000"}, {"Type": "Label", "Index": 13, "AliasName": "echo_text", "ParentName": "Form_1", "PlaceInfo": [356, 308, 230, 48, "nw", true, true], "Visible": true, "Size": [230, 48], "BGColor": "#EFEFEF", "Text": "不替换文件名", "FGColor": "#000000", "Anchor": "e"}, {"Type": "Button", "Index": 9, "AliasName": "replace_button", "ParentName": "Form_1", "PlaceInfo": [69, 308, 189, 48, "nw", true, true], "Visible": true, "Size": [189, 48], "BGColor": "#EFEFEF", "Text": "开始替换", "FGColor": "#000000", "Relief": "raised", "EventList": {"Command": "replace_button_onCommand"}}, {"Type": "Button", "Index": 11, "AliasName": "browser", "ParentName": "Form_1", "PlaceInfo": [622, 34, 119, 48, "nw", true, false], "Visible": true, "Size": [119, 48], "BGColor": "#EFEFEF", "Text": "浏览", "FGColor": "#000000", "Relief": "raised", "EventList": {"Command": "browser_onCommand"}}, {"Type": "SwitchButton", "Index": 12, "AliasName": "file_name", "ParentName": "Form_1", "PlaceInfo": [596, 313, 111, 38, "nw", true, true], "Visible": true, "Size": [111, 38], "Shape": "circular", "BGColor_Off": "#333333", "Text_Off": "", "FGColor_Off": "#FFFFFF", "BtnColor_Off": "#FFFFFF", "BGColor_On": "#2F9F00", "Text_On": "", "FGColor_On": "#FFFFFF", "BtnColor_On": "#FFFFFF", "Font": ["Microsoft YaHei UI", 9, "normal", "roman", 0, 0], "Value": false, "SwitchMode": "0", "EventList": {"Switch": "file_name_onSwitch"}}, {"Type": "Text", "Index": 14, "AliasName": "output", "ParentName": "Form_1", "PlaceInfo": [71, 401, 670, 607, "nw", true, true], "Visible": true, "Size": [670, 607], "AutoWrap": true, "Relief": "sunken"}]}'
        Form_1 = Fun.CreateUIFormJson(uiName,root,isTKroot,style,self.UIJsonString)
        #Inital all element's Data 
        Fun.InitElementData(uiName)
        #Call Form_1's OnLoad Function
        Fun.RunForm1_CallBack(uiName,"Load",ReplaceProjectStr_cmd.Form_1_onLoad)
        #Add Some Logic Code Here: (Keep This Line of comments)



        #Exit Application: (Keep This Line of comments)
        if self.isTKroot == True and Fun.GetElement(self.uiName,"root"):
            self.root.protocol('WM_DELETE_WINDOW', self.Exit)
            self.root.bind('<Configure>', self.Configure)
            
    def GetRootSize(self):
        return Fun.GetUIRootSize(self.uiName)
    def GetAllElement(self):
        return Fun.G_UIElementDictionary[self.uiName]
    def Escape(self,event):
        if Fun.AskBox('提示','确定退出程序？') == True:
            self.Exit()
    def Exit(self):
        if self.isTKroot == True:
            Fun.DestroyUI(self.uiName,0,'')

    def Configure(self,event):
        Form_1 = Fun.GetElement(self.uiName,'Form_1')
        if Form_1 == event.widget:
            Fun.ReDrawCanvasRecord(self.uiName)
        if self.root == event.widget and (self.configure_event is None or self.configure_event[2]!= event.width or self.configure_event[3]!= event.height):
            uiName = self.uiName
            self.configure_event = [event.x,event.y,event.width,event.height]
            Fun.ResizeRoot(self.uiName,self.root,event)
            Fun.ResizeAllChart(self.uiName)
            pass
#Create the root of tkinter 
if  __name__ == '__main__':
    Fun.RunApplication(ReplaceProjectStr)
